package karaokestrong.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import karaokestrong.excepcion.DAOExcepcion;
import karaokestrong.modelo.Administrador;
import karaokestrong.modelo.Persona;
import karaokestrong.util.ConexionBD;

public class PersonaDAO extends BaseDAO {

	
public Persona insertar(Persona vo) throws DAOExcepcion {
		
		System.out.println("PersonaDAO: insertar(Persona vo)");
		String query = "INSERT INTO persona(dni,nombres,paterno,materno) VALUES (?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			
			
			stmt.setInt(1, vo.getDni());
			stmt.setString(2, vo.getNombres());
			stmt.setString(3, vo.getPaterno());
			stmt.setString(4, vo.getMaterno());
			System.out.println("---------------------");
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			
	
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}


public Collection<Persona> ListarPersona() throws DAOExcepcion {
	System.out.println("Admin: ListarPersona()");
	Collection<Persona> c = new ArrayList<Persona>();
	Connection con = null;
	PreparedStatement stmt = null;
	ResultSet rs = null;
	try {
		con = ConexionBD.obtenerConexion();

		String query = "select * from persona";
		stmt = con.prepareStatement(query);
		rs = stmt.executeQuery();

		while (rs.next()) {
			Administrador vo = new Administrador();

		
			vo.setDni(rs.getInt("dni"));

			vo.setNombres(rs.getString("nombres"));
			vo.setPaterno(rs.getString("paterno"));
			vo.setMaterno(rs.getString("materno"));

			c.add(vo);
		}
	} catch (SQLException e) {
		System.err.println(e.getMessage());
		throw new DAOExcepcion(e.getMessage());
	} finally {
		this.cerrarStatement(stmt);
		this.cerrarConexion(con);
	}
	return c;
}
}
